package com.gree.changying.activity.mainactivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.gree.changying.R;
import com.gree.changying.activity.base.BasePageActivity;
import com.gree.changying.activity.mainactivity.adapter.FrmPagerAdapter;
import com.gree.changying.activity.mainactivity.framgent.HomeFrm;
import com.gree.changying.activity.mainactivity.framgent.MyFrm;
import com.gree.changying.activity.mainactivity.framgent.PhFrm;
import com.gree.changying.activity.mainactivity.framgent.SearchFrm;
import com.gree.changying.widget.tablayout.jtablayout.tab.Tab;
import com.gree.changying.widget.tablayout.widget.JTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BasePageActivity {

    @Override
    protected boolean setEnableSliding() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        hideHeader();
        initPage();
    }

    private Object[][] tabMenu = {
            {R.drawable.icon_home,R.drawable.icon_home_p,"首页"},
            {R.drawable.icon_ph,R.drawable.icon_ph_p,"排行"},
            {R.drawable.icon_search,R.drawable.icon_search_p,"搜索"},
            {R.drawable.icon_my,R.drawable.icon_my_p,"我的"},
    };
    private FrmPagerAdapter mAdapter;
    private List<Fragment> mFragments;
    @Bind(R.id.mTabLayout) JTabLayout mTabLayout;
    @Bind(R.id.mViewPager)
    ViewPager mViewPager;
    private HomeFrm homeFrm;
    private PhFrm phFrm;
    private SearchFrm searchFrm;
    private MyFrm myFrm;
    private void initPage(){
        mFragments = new ArrayList<>();
        homeFrm = new HomeFrm();
        phFrm = new PhFrm();
        searchFrm = new SearchFrm();
        myFrm = new MyFrm();
        mFragments.add(homeFrm);
        mFragments.add(phFrm);
        mFragments.add(searchFrm);
        mFragments.add(myFrm);
        FragmentManager fragmentManager  = getSupportFragmentManager();
        mAdapter = new FrmPagerAdapter(fragmentManager,mFragments);

        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(4);
        mViewPager.setCurrentItem(0);
        for(int i = 0 ;i<tabMenu.length;i++){
            Tab tab = mTabLayout.newTab().setTitle((String)tabMenu[i][2]);
            tab.setBadgeBackgroundColor(getResources().getColor(R.color.holo_red));
            tab.setIcon((int)tabMenu[i][0],(int)tabMenu[i][1]);
            tab.setTitleColor(getResources().getColor(R.color.tabColor),
                    getResources().getColor(R.color.themeColor));
            mTabLayout.addTab(tab);
        }
        initListener();
    }
    private void initListener(){
        mTabLayout.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(@NonNull Tab var1) {
                int position = var1.getPosition();
                if(position!=mViewPager.getCurrentItem()) {
                    mViewPager.setCurrentItem(position);
                }
            }

            @Override
            public void onTabUnselected(@NonNull Tab var1) {

            }

            @Override
            public void onTabReselected(@NonNull Tab var1) {

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==0||position==3){
                    setStatusBar(R.color.themeColor,false);
                }else{
                    setStatusBar(R.color.white,true);
                }
                mTabLayout.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public boolean setStatusBar() {
        return false;
    }

    @Override
    public boolean onFinish() {
        return false;
    }


    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                shortToast("再按一次返回桌面");
                exitTime = System.currentTimeMillis();
            } else {
                moveTaskToBack(true);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onRightBtnClick(View view) {

    }
}